package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IProdInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ResourcePreemptionHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    public IProdInstService prodInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String offerId = querySceneInstService.getOfferInst(kiteSceneInst.getCustId(), kiteSceneInst.getRootInstId()).getOfferId();

        if(KiteStringUtils.isEqual(offerId, "50040853")) {
            // 国际互联网，不展示 ORD10004-资源是否预占
            commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            commonAttr.setDefaultValue(null);
            commonAttr.setValue(null);

            KiteAttr kiteAttr = querySceneInstService.getCommonAttr(sceneInstId, commonAttr.getFieldName());
            kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            kiteAttr.setDefaultValue(null);
            kiteAttr.setValue(null);
            operateSceneCommonService.setCommonAttr(sceneInstId, kiteAttr);
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 订单信息（资源核查） 资源预占期
        KiteAttr orderResourceAttr = querySceneInstService
            .getCommonAttrsByFieldName(sceneInstId, "ORD10013", "204");
        if (Objects.isNull(orderResourceAttr)) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        // 当资源是否预占选择是的展示且必填，否则不展示
        if (KiteStringUtils.equals("1", value)) {
            orderResourceAttr.setShowType("D");
            orderResourceAttr.setIsNull("F");
        } else {
            orderResourceAttr.setShowType("H");
            orderResourceAttr.setIsNull("T");
        }
        operateSceneCommonService.setCommonAttr(sceneInstId, orderResourceAttr);
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
